<?php

// $Id$


require_once($WSC->get_site_inc_dir().'modules/mailers_abstract/mailer_advanced.class.php');
require_once($WSC->get_ext_inc_dir().'extlib_swift_mailer/lib/Swift.php');
require_once($WSC->get_ext_inc_dir().'extlib_swift_mailer/lib/Swift/Connection/SMTP.php');


class Mailer_Swift extends Mailer_Advanced {

	public function send() {
		$ret = true;

		$this->set_error_msg('');

		$smtp = $this->prepare_smtp();
		$message = $this->prepare_message();
		$recipients = $this->prepare_recipients();
		$from = $this->split_email_address($this->get_from());

		$swift = new Swift($smtp);

		try {
			$rez = $swift->send($message, $recipients, new Swift_Address($from['address'], $from['name']) );
			$this->log_sent($rez);
		} catch (Swift_ConnectionException $e) {
			$ret = false;
			$this->set_error_msg("There was a problem communicating with SMTP: " . $e->getMessage());
		} catch (Swift_Message_MimeException $e) {
			$ret = false;
			$this->set_error_msg("There was an unexpected problem building the email:" . $e->getMessage());
		}

		$swift->disconnect();

		return $ret;
	}


	private function prepare_smtp() {
		$smtp_settings = $this->get_smtp_settings();
		$smtp = new Swift_Connection_SMTP($smtp_settings->get_host(), $smtp_settings->get_port(), $smtp_settings->get_encryption());
		if ($smtp_settings->get_use_auth()) {
			$smtp->setUsername($smtp_settings->get_username());
			$smtp->setpassword($smtp_settings->get_password());
		}
		$smtp->setTimeout($smtp_settings->get_timeout());

		return $smtp;
	}


	private function prepare_message() {
		$message = new Swift_Message($this->get_subject());

		switch($this->get_format()) {
			case Mailer_Advanced::MSG_TEXT:
				$message->setBody($this->get_body());
				$message->setContentType('text/plain');
				$message->setEncoding($this->get_encoding());
				break;
			case Mailer_Advanced::MSG_HTML:
				$message->setBody($this->get_body_html());
				$message->setContentType('text/html');
				$message->setEncoding($this->get_encoding());
				break;
			case Mailer_Advanced::MSG_BOTH:
				$message->attach(new Swift_Message_Part($this->get_body(), 'text/plain', $this->get_encoding()));
				$message->attach(new Swift_Message_Part($this->get_body_html(), "text/html", $this->get_encoding()));
				break;
		}


		foreach($this->get_images() as $image) {
			$message->attach(new Swift_Message_Image(new Swift_File($image->get_file()),  $image->get_name(), $image->get_type(), $image->get_cid()));
		}


		foreach($this->get_attachments() as $attachment) {
			$message->attach(new Swift_Message_Attachment(new Swift_File($attachment->get_file()),  $attachment->get_name(), $attachment->get_type(), $attachment->get_encoding(), $attachment->get_content_disposition() ));
		}

		if ($this->get_return_path()) {
			$message->setReturnPath($this->get_return_path());
		}


		if ($this->get_reply_to()) {
			$message->setReplyTo($this->get_reply_to());
		}


		return $message;
	}


	private function prepare_recipients() {
		$recipients = new Swift_RecipientList();
		foreach($this->get_tos() as $to) {
			$splited = $this->split_email_address($to);
			$recipients->addTo($splited['address'], $splited['name'] ? $splited['name'] : NULL);
		}

		foreach($this->get_ccs() as $cc) {
			$splited = $this->split_email_address($cc);
			$recipients->addCc($splited['address'], $splited['name'] ? $splited['name'] : NULL);
		}

		foreach($this->get_bccs() as $cc) {
			$splited = $this->split_email_address($cc);
			$recipients->addBcc($splited['address'], $splited['name'] ? $splited['name'] : NULL);
		}

		return $recipients;
	}


	private function split_email_address($email) {
		$ret = array();

		$pos = strpos($email, '<');
		if ($pos !== false) {
			$ret['name'] = trim(substr($email, $pos));
			$ret['address'] = substr($email, $pos + 1, strlen($email) - ($pos + 1) - 1);
		} else {
			$ret['address'] = $email;
			$ret['name'] = NULL;
		}

		return $ret;
	}
}